/*********************************************************************
	\file :           os_sw_layers\bios\mcu\Mcu.c
	\brief:           main application.
	\author:          Manuel Sandoval
	\version:         1.0
	\date:            Sat, 01, March 2014
*********************************************************************/


/******************************* Includes**********************************/

#include <hidef.h>      			/*!< common defines and macros */
#include "derivative.h"      		/*!< derivative-specific definitions */

#include "Mcu.h"
#include "Gpt.h"

#include <string.h>
#include "xgate.h"


/******************************* Typedef **********************************/

#pragma push

/*!< this variable definition is to demonstrate how to share data between XGATE and S12X */
#pragma DATA_SEG SHARED_DATA
volatile int shared_counter; 			/*!< volatile because both cores are accessing it. */

/*!< Two stacks in XGATE core3 */
#pragma DATA_SEG XGATE_STK_L
word XGATE_STACK_L[1];
#pragma DATA_SEG XGATE_STK_H
word XGATE_STACK_H[1];

#pragma pop


/******************************* Defines **********************************/

#define ROUTE_INTERRUPT(vec_adr, cfdata)                \
  INT_CFADDR= (vec_adr) & 0xF0;                         \
  INT_CFDATA_ARR[((vec_adr) & 0x0F) >> 1]= (cfdata)

#define SOFTWARETRIGGER0_VEC  0x72 						/*!< vector address= 2 * channel id */


/******************************* Enumerations******************************/


/******************************* Structures *******************************/


/******************************* Global Variables **********************************/


/******************************* Prototypes ********************************/


/******************************* Functions ********************************/


/***************************************************************************
*
*   FUNCTION NAME: SetupXGATE
*
***************************************************************************/
/**
* @brief Set up the XGATE mcu initialization.
*
* This function allows the application to set the XGATE initialization.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/

static void SetupXGATE(void)
{
	/*!< initialize the XGATE vector block and
     set the XGVBR register to its start address */
  XGVBR= (unsigned int)(void*__far)(XGATE_VectorTable - XGATE_VECTOR_OFFSET);

  /*!< switch software trigger 0 interrupt to XGATE */
  ROUTE_INTERRUPT(SOFTWARETRIGGER0_VEC, 0x81); /* RQST=1 and PRIO=1 */

  /*!< when changing your derivative to non-core3 one please remove next five lines */
  XGISPSEL= 1;
  XGISP31= (unsigned int)(void*__far)(XGATE_STACK_L + 1);
  XGISPSEL= 2;
  XGISP74= (unsigned int)(void*__far)(XGATE_STACK_H + 1);
  XGISPSEL= 0;

  /*!< enable XGATE mode and interrupts */
  XGMCTL= 0xFBC1; /* XGE | XGFRZ | XGIE */

  /*!< force execution of software trigger 0 handler */
  XGSWT= 0x0101;

}



/***************************************************************************
*
*   FUNCTION NAME: SetupXGATE
*
***************************************************************************/
/**
* @brief main application.
*
* This function allows the application to set all mcu initialization.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/

void main(void)
{
	/*!< Generated by codewarrior */
	SetupXGATE();

	/*!< Enable MCU interrupts */
	EnableInterrupts;

	IVBR = 0xFFu;

	/*!< Init Mcu */
	Mcu_Init();

	/*!<  Distribute pll clock */
	Mcu_DistributePllClock();
	
	/*!< Gpt init */
	Gpt_Init(Mcal_Gpt_Config);

	/*!< Gpt start timer */
	Gpt_StartTimer(0, 0);
	
	/*!< Gpt init */
	App_SetUpTest1();


  for(;;)
  {
    _FEED_COP(); /* feeds the dog */
  } /* loop forever */

  /* please make sure that you never leave main */
}


/** @} *//* End of main Group */


/*********************************************************************
	File	: Sources\main.c
*********************************************************************/

